package com.hxzy.common.util;


import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局上下文工具类
 * @Author: wy
 */
@Component
public class RequestContexUtil {



    /**
     * 获取请求request
     * @return
     */
    public static HttpServletRequest getRequest(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 获取请求response
     * @return
     */
    public static HttpServletResponse getResponse(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return response;
    }
    /**
     * 获取请求sessionId
     * @return
     */
    public static String getTacerId(){
        return getValueFromHeader("tracer_id");
    }

    /**
     * 获取请求头中任意的值
     * @param key
     * @return
     */
    public static String getValueFromHeader(String key){
        HttpServletRequest request=getRequest();
        return request.getHeader(key);
    }


}
